/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.locationbasedagent;

import java.util.List;

import com.locationbasedagent.shared.HouseListingProxy;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class SearchByListActivity extends Activity {
    /**
     * Tag for logging.
     */
    private static final String TAG = "SearchByListActivity";
    
    private Button mListResultButton;
    private Button mMapResultButton;

    private EditText mAddressTextField;
    private EditText mCityTextField;
    private EditText mZipTextField;
    
    private String radiusSelected = "0";

    /**
     * The current context.
     */
    private Context mContext = this;
    
   
    
    /**
     * 
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.searchbylist);
        
        
        //set the values for the radius location search
        Spinner spinner = (Spinner) findViewById(R.id.radiusSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.mileage_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                radiusSelected = parent.getItemAtPosition(pos).toString();
                String[] tokens = radiusSelected.split(" ");
                radiusSelected = tokens[0];
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        
        //setup the button event handlers
        mListResultButton = (Button) findViewById(R.id.listResultButton);
        mListResultButton.setOnClickListener(mListResultButtonListener);
        mMapResultButton = (Button) findViewById(R.id.mapResultButton);
        mMapResultButton.setOnClickListener(mMapResultButtonListener);
        
        //setup the edit text fields
       
        mAddressTextField = (EditText) findViewById(R.id.streetAddressEditText);
        mCityTextField = (EditText) findViewById(R.id.cityEditText);
        mZipTextField = (EditText) findViewById(R.id.zipCodeEditText);
    }
    
    private OnClickListener mListResultButtonListener = new OnClickListener() {
        public void onClick(View v) {        	
            Intent intent =
                    new Intent(SearchByListActivity.this,
                            ResultListActivity.class);
            Bundle bundle = new Bundle();
            
            bundle.putString("address", mAddressTextField.getText().toString());
            bundle.putString("city", mCityTextField.getText().toString());
            bundle.putString("zip", mZipTextField.getText().toString());
            intent.putExtras(bundle); 
            startActivity(intent);

        }
    };
    private OnClickListener mMapResultButtonListener = new OnClickListener() {
        public void onClick(View v) {        	
            Intent intent =
                    new Intent(SearchByListActivity.this,
                            SearchByLocationActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("address", mAddressTextField.getText().toString());
            bundle.putString("city", mCityTextField.getText().toString());
            bundle.putString("zip", mZipTextField.getText().toString());
            bundle.putString("radius", radiusSelected);
            intent.putExtras(bundle);
            startActivity(intent);

        }
    };

    
}